﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace EShop.Net.IRepository
{
    /// <summary>
    /// 仓储通用接口类
    /// 只提供了基础的增删改查，涉及复杂增删改查，请在仓储层写相关方法
    /// </summary>
    /// <typeparam name="T">泛型实体类</typeparam>
    public interface IBaseRepository<T> where T : class, new()
    {
        /// <summary>
        /// 根据ID，查询单条数据(同步)
        /// </summary>
        /// <param name="pkValue">ID</param>
        /// <returns>泛型实体</returns>
        T QueryById(object pkValue);

        /// <summary>
        /// 根据ID，查询单条数据(异步)
        /// </summary>
        /// <param name="objId">ID</param>
        /// <returns>泛型实体</returns>
        Task<T> QueryByIdAsync(object objId);

        /// <summary>
        /// 查询所有数据，无分页小小使用(同步)
        /// </summary>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        List<T> Query(bool blUseNoLock = false);

        /// <summary>
        /// 查询所有数据，无分页小小使用(异步)
        /// </summary>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        Task<List<T>> QueryAsync(bool blUseNoLock = false);

        /// <summary>
        /// 根据条件查询数据(同步)
        /// </summary>
        /// <param name="predicate">条件表达式树</param>
        /// <param name="orderBy">排序</param>
        /// <returns>泛型实体集合</returns>
        List<T> QueryListByClause(Expression<Func<T, bool>> predicate, string orderBy = "");

        /// <summary>
        /// 根据条件查询数据(异步)
        /// </summary>
        /// <param name="predicate">条件表达式树</param>
        /// <param name="orderBy">排序</param>
        /// <returns>泛型实体集合</returns>
        Task<List<T>> QueryListByClauseAsync(Expression<Func<T, bool>> predicate, string orderBy = "");

        /// <summary>
        /// 写入实体数据(同步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>受影响行数</returns>
        int Insert(T entity);

        /// <summary>
        /// 写入实体数据(异步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>受影响行数</returns>
        Task<int> InsertAsync(T entity);

        /// <summary>
        /// 更新实体数据(同步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>成功或失败</returns>
        bool Update(T entity);

        /// <summary>
        /// 更新实体数据(异步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>成功或失败</returns>
        Task<bool> UpdateAsync(T entity);

        /// <summary>
        /// 删除指定ID的数据(同步)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>成功或失败</returns>
        bool DeleteById(object id);

        /// <summary>
        /// 删除指定ID的数据(异步)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>成功或失败</returns>
        Task<bool> DeleteByIdAsync(object id);

        /// <summary>
        /// 执行预处理sql语句并返回List<T>
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">预处理对象</param>
        /// <returns></returns>
        List<T> SqlQueryPara(string sql, List<SugarParameter> parameters);

        /// <summary>
        /// 执行sql语句并返回List<T>
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        Task<List<T>> SqlQuery(string sql);
    }
}
